/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * libburmese - Burmese Language Support Library
 * Copyright (C) 2011  Thura Hlaing <trhura@gmail.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

/*
 * test_mystr.c - Tests for mystr.h
 * Created: 2011-01-08
 */

#include <burmese/mystr.h>
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#include <assert.h>
#include <string.h>
#include <wchar.h>

#define SYLLABLE_FILE "cluster-test-strings"

int main(int argc, char *argv[])
{
    setlocale(LC_CTYPE, "");

    FILE *file = fopen (SYLLABLE_FILE, "r");

    if (file == NULL) {
        printf ("tes cluster string file not found.\n");
        return -1;
    }

    wchar_t *tmp = (wchar_t*)malloc (sizeof (wchar_t) * 32);
    wchar_t *wholestr   = (wchar_t*)malloc (sizeof (wchar_t) * 2560);
    wchar_t **str_array = (wchar_t**)malloc (sizeof (wchar_t*) * 128);

    int i = 0;
    while ((tmp = fgetws (tmp, 64,  file))) {
        tmp[wcslen(tmp)-1] = L'\0';
        //printf ("%ls\n", tmp);
        wcscat (wholestr, tmp);
        str_array[i] = wcsdup(tmp);
        i++;
    }
    //printf ("%ls\n", wholestr);
    int lines = i;
    i = 0;

    ClusterIter *iter = mystr_cluster_iter_new (wholestr);
    
    while (iter != NULL & i < lines) {
        wchar_t *cluster;        
        cluster = mystr_cluster_iter_next (iter);
        //fprintf (stdout, "%ls %ls\n", cluster, str_array[i]);
        printf ("testing cluster break %ls....\n", str_array[i]);
        if (wcscmp (str_array[i], cluster) != 0) {
            printf ("test failed at %ls.\n", cluster);
            return -1;
        }
        free (cluster);
        free (str_array[i]);
        i++;
    }

    free (tmp); 
    free (str_array);
    mystr_cluster_iter_free (iter);
    return 0;
}
